package com.hpp.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
	public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
	/** 完整时间 yyyy-MM-dd HH:mm:ss */
	public static final String simple = "yyyy-MM-dd HH:mm:ss";
	public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
	public static final String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String HHmm = "HH:mm";
	public static final String MMdd = "MM月dd";
	public static final String yyyyMM = "yyyy年MM";
	public static final String yyyy = "yyyy年";
	public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String YMDHMS2 = "yyyy/MM/dd HH:mm:ss";

	/**
	 * 获取日期(精确到毫秒)格式化，格式：yyyy-MM-dd HH:mm:ss
	 * 
	 * @return
	 */
	public static String getDateFormatter(Date date) {
		DateFormat df = new SimpleDateFormat(simple);
		return df.format(date);
	}

	public static Date parseDate(String time, String pattern) {
		DateFormat df = new SimpleDateFormat(simple);
		Date date = null;
		try {
			date = df.parse(time);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return date;
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 
	 * getHistory:获取11个月前的日期  
	 *  
	 * @author cj  
	 * 2018年2月22日
	 * @return
	 */
	public static String getHistory() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, -11);
		return formatDate(cal.getTime(),yyyyMM);
	}

	public static void main(String[] args) throws ParseException {
		/*
		 * String time = "2015-03-14 10:22:22"; SimpleDateFormat sdf = new
		 * SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = sdf.parse(time);
		 * DateTime dateTime = new DateTime(date);
		 * System.out.println(dateTime.dayOfWeek().withMinimumValue());
		 */
		/*
		 * String time = "2015-03-14"; SimpleDateFormat sdf = new
		 * SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(time); DateTime
		 * dateTime = new DateTime(date); int dayOfWeek = dateTime.dayOfWeek().get();
		 * System.out.println(dateTime.minusDays(dayOfWeek));
		 */
		// System.out.println(new );
		/*
		 * System.out.println(DateUtils.buildBetweenCond("time", sdf.parse(time),
		 * TimeGrain.Year, 1));
		 * 
		 * Date date = DateUtils.getDateFrom2010sec(157766400);
		 * System.out.println(sdf.format(date));
		 */
		String pattern = "yyyy-MM-dd HH:mm:ss";
		Date date1 = new Date();
		Date date2 = new Date();
		date1.setTime(1423353600000l);
		date2.setTime(1420056000000l);
		System.out.println(DateUtils.formatDate(date1, pattern));
		System.out.println(DateUtils.formatDate(date2, pattern));
	}
	
	public static String getFormatDateString(String dateStr) {
		DateFormat df = new SimpleDateFormat(YMDHMS);
		try {
			Date date = df.parse(dateStr);
			return df.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
			df = new SimpleDateFormat(YMDHMS2);
			Date date;
			try {
				date = df.parse(dateStr);
				return df.format(date);
			} catch (ParseException e1) {
				e1.printStackTrace();
				return "";
			}
		}
	}

}
